package com.sui.bigdata.rtcadmin.service.impl;

import com.sui.bigdata.rtcadmin.exception.ResourceException;
import com.sui.bigdata.rtcadmin.model.dto.ResourceInfoDto;
import com.sui.bigdata.rtcadmin.model.vo.CommonResponse;
import com.sui.bigdata.rtcadmin.repository.mapper.ResourceInfoMapper;
import com.sui.bigdata.rtcadmin.repository.model.ResourceInfo;
import com.sui.bigdata.rtcadmin.service.ResourceInfoService;
import com.sui.bigdata.rtcadmin.util.HdfsUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author: TingWuHuang
 * @Date: 2020/7/2 11:08
 * @description
 */
@Service
public class ResourceInfoServiceImpl implements ResourceInfoService {
    Logger logger = LoggerFactory.getLogger(ResourceInfoServiceImpl.class);

    @Autowired
    ResourceInfoMapper resourceInfoMapper;

    @Override
    public CommonResponse add(MultipartFile file, String remarks, String fileName, String resource) {
        if (StringUtils.isEmpty(fileName)) {
            fileName = file.getOriginalFilename();
        }
        ResourceInfo resourceInfo;
        resourceInfo = resourceInfoMapper.selectByFileName(fileName);
        if(resourceInfo != null){
            throw new ResourceException(fileName + " is exist!");
        }
        long fileSize = file.getSize();
        try {
            InputStream inputStream = file.getInputStream();
            String filePath = HdfsUtils.uploadFileByStream(fileName, inputStream);
            resourceInfo = new ResourceInfo( fileName, filePath, resource, fileSize, remarks);
            resourceInfoMapper.insert(resourceInfo);
        } catch (IOException e) {
            throw new ResourceException("file upload fail!");
        }
        resourceInfo = resourceInfoMapper.selectByFileName(fileName);

        return CommonResponse.Builder.buildSuccess(resourceInfo);
    }

    @Override
    public CommonResponse update(MultipartFile file, String remarks, String fileName, String resource) {

        ResourceInfo resourceInfo = resourceInfoMapper.selectByFileName(fileName);
        long fileSize = file.getSize();
        try {
            InputStream inputStream = file.getInputStream();
            String filePath = HdfsUtils.uploadFileByStream(fileName, inputStream);
            resourceInfo.setFileSize(fileSize);
            resourceInfo.setFilePath(filePath);
            if(StringUtils.isNotEmpty(remarks)){
                resourceInfo.setRemarks(remarks);
            }
            if(StringUtils.isNotEmpty(resource)){
                resourceInfo.setResource(resource);
            }
            resourceInfoMapper.update(resourceInfo);
        } catch (IOException e) {
            throw new ResourceException("file upload fail!");
        }
        return CommonResponse.Builder.buildSuccess(resourceInfo);
    }

    @Override
    public CommonResponse deleteFile(String fileName) {
        try {
            HdfsUtils.deleteFile(fileName);
            resourceInfoMapper.delete(fileName);
        } catch (IOException e) {
            logger.warn("file: {} delete fail!", fileName);
            throw new ResourceException("file delete fail!");
        }

        return CommonResponse.Builder.buildSuccess(null);
    }

    @Override
    public CommonResponse select(ResourceInfoDto resourceInfoDto) {
        List<ResourceInfo> resourceInfo = resourceInfoMapper.select(ResourceInfo.buildResourceInfo(resourceInfoDto));

        return  CommonResponse.Builder.buildSuccess(resourceInfo);
    }
}
